package com.ay.weixin.fdc.boss.share.event;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;

import org.apache.commons.fileupload.FileItem;
import org.lizar.config.Config;
import org.lizar.config.keys.Keys;
import org.lizar.controller.Event;
import org.lizar.controller.loader.EventLoader;
import org.lizar.controller.loader.FilePostForm;
import org.lizar.ioc.Container;
import org.lizar.util.StringHelper;

import com.ay.weixin.config.AppConfig;
import com.ay.weixin.config.ProjectConfig;
import com.ay.weixin.fdc.admin.customer.domain.Customer;
import com.ay.weixin.fdc.boss.autoreply.event.ReplySetting;
import com.ay.weixin.fdc.boss.customer.event.CustomerLogin;
import com.ay.weixin.fdc.boss.share.db.ShareActivityContentDao;
import com.ay.weixin.fdc.boss.share.db.ShareActivityDao;
import com.ay.weixin.fdc.boss.share.db.ShareBackHitsDao;
import com.ay.weixin.fdc.boss.share.db.ShareRecordDao;
import com.ay.weixin.fdc.boss.share.domain.ShareActivity;
import com.ay.weixin.fdc.boss.share.domain.ShareActivityContent;
import com.ay.weixin.fdc.boss.share.domain.ShareBackHits;

public class MCShare extends Event {

	private ShareActivityDao share_dao;
	private ShareActivityContentDao share_content_dao;
	private ShareBackHitsDao share_back_hits_dao;
	private ShareRecordDao share_record_dao;
	
	@Override
	public void init_property() throws Exception {
		share_dao=Container.get(ShareActivityDao.class);
		share_content_dao=Container.get(ShareActivityContentDao.class);
		share_back_hits_dao=Container.get(ShareBackHitsDao.class);
		share_record_dao=Container.get(ShareRecordDao.class);
		
		
		
	}

	@Override
	public String default_handling_path() {
		return "/mc/share/*";
	}

	@Override
	public void handle(EventLoader el) throws ServletException, IOException {
		Customer adm=(Customer)el.getSessionAttr("customer");
		if(adm==null){
			el.response_to_root(CustomerLogin.login_path());
		}else{
			if(el.queryPathCheck(2, "")){
				index(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "add_new")){
				add_new(el);
			}else if(el.queryPathCheck(2, "add")){
				add(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "visit")){
				visit(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "back_hits")){
				back_hits(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "add_new_message")){
				add_new_message(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "update_message")){
				update_message(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "update")){
				update(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "append_hits")){
				append_hits(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "end_now")){
				end_now(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "start_now")){
				start_now(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "archive_now")){
				archive_now(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "published")){
				published(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "announcement")){
				announcement(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "announcement_now")){
				announcement_now(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "announcement_abort")){
				announcement_abort(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "delete")){
				delete(el,adm.getWxprojectID());
			}else if(el.queryPathCheck(2, "delete_message")){
				delete_message(el,adm.getWxprojectID());
			}
			
		}
	}
	
	private void delete_message(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("message_id");
		long sa_id=el.getLong("sa_id");
		el.setParam("id",sa_id);
		
		try {
			share_content_dao.delete(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			visit(el,wxproject_id);
			return;
		}
		
		visit(el,wxproject_id);
	}
	
	private void delete(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("id");
		ShareActivity share=null;
		
		try {
			share=share_dao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		
		if(share==null){
			el.setAttr("msg",el.i18("mc.share.not.exists"));
			index(el,wxproject_id);
			return;
		}
		
		if(share.getStatus()!=ShareActivity.STATUS.CLOSED.getValue()){
			el.setAttr("msg",el.i18("mc.share.only.closed.canbe.deleted"));
			index(el,wxproject_id);
			return;
		}
		
		List<ShareActivityContent> content_list=null;
		
		try {
			content_list=share_content_dao.getBySAId(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		
		try {
			share_record_dao.deleteBySaId(id);
			share_content_dao.deleteBySaId(id);
			share_back_hits_dao.deleteBySaId(id);
			share_dao.delete(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		
		if(content_list!=null){
			for(ShareActivityContent content:content_list){
				if(StringHelper.isNotNull(content.getPic_url())){
					File pic=new File(el.getRealPath(content.getPic_url()));
					pic.delete();
				}
			}
		}
		el.setAttr("msg",el.i18("operation.success"));
		index(el,wxproject_id);
	}
	
	
	private void published(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("id");
		try {
			share_dao.published(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("operation.success"));
		index(el,wxproject_id);
	}
	
	private void archive_now(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("id");
		try {
			share_dao.archive_now(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("operation.success"));
		index(el,wxproject_id);
	}
	
	private void start_now(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("id");
		try {
			share_dao.start(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("operation.success"));
		index(el,wxproject_id);
	}
	
	private void end_now(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("id");
		try {
			share_dao.stop(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("operation.success"));
		index(el,wxproject_id);
	}
	
	private void append_hits(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getAndSetLong("id");
		long hit_id=el.getAndSetLong("hit_id");
		int hits=el.getAndSetInt("hits");
		
		try {
			share_back_hits_dao.append_hits(el.getParams());
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("operation.success"));
		
		back_hits(el,wxproject_id);
	}
	
	private void announcement(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("id");
		long hit_id=el.getLong("hit_id");
		String reward=el.getStr("reward");
		try {
			share_back_hits_dao.update_reward(hit_id,reward);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("server.error")+":"+e.getLocalizedMessage());
			announcement_now(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("operation.success"));
		announcement_now(el,wxproject_id);
	}
	
	private void announcement_abort(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("id");
		long hit_id=el.getLong("hit_id");
		try {
			share_back_hits_dao.update_reward(hit_id,null);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("server.error")+":"+e.getLocalizedMessage());
			announcement_now(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("operation.success"));
		announcement_now(el,wxproject_id);
	}
	
	private void update_message(EventLoader el,long wxproject_id) throws IOException, ServletException{
		if(!el.isMultipartContent()){
			el.text("invalid visit.");
			return;
		}
		FilePostForm file=el.parseFilePostForm(ProjectConfig.getLong(wxproject_id,"sys.max_image_file_size")*1024);
		file.setAllParamToAttr(el);
		String content=file.getStr("content");
		long id=file.getLong("id");
		long sa_id=file.getLong("sa_id");
		int pos=file.getInt("pos");
		el.setParam("id", sa_id);
		String msg=null;
		if(file.getErrorMsg()!=null){
	        msg=file.getErrorMsg();
	        el.setAttr("msg",msg);
	        visit(el,wxproject_id);
	       return;
	    }
		
		ShareActivityContent share_content=null;
		
		try {
			share_content=share_content_dao.getById(id);
		} catch (SQLException e1) {
			e1.printStackTrace();
			el.setAttr("msg",el.i18("server.error")+":"+e1.getLocalizedMessage());
			visit(el,wxproject_id);
			return;
		}
		if(share_content==null){
			el.setAttr("msg",el.i18("mc.share.sharecontent.not.exists"));
			visit(el,wxproject_id);
			return;
		}
		
		String pic_file_name=null;
		FileItem pic=null;
		File pic_file=null;
		List<FileItem> items=file.getFileItems();
		for(FileItem it:items){
			if(it.getFieldName().toLowerCase().equals("pic")){
				pic=it;
			}
		}
		
		if(pic!=null){
			if(AppConfig.img_support_fix.get(StringHelper.postfix(pic.getName().toLowerCase()))==null){
				msg=el.i18("mc.share.pic.format.not.corrent");
				el.setAttr("msg",msg);
				visit(el,wxproject_id);
				return;
			}else{
				pic_file_name=UUID.randomUUID().toString()+".jpg";
		        String file_path=ReplySetting.getImagePath(el,wxproject_id, pic_file_name);
		        pic_file=new File(file_path);
		        if(!pic_file.getParentFile().exists()){
		        	pic_file.getParentFile().mkdirs();
		        }
		        try {
		        	pic.write(pic_file);
				} catch (Exception e) {
					e.printStackTrace();
					msg=el.i18("image.save.failed");
					el.setAttr("msg",msg);
					visit(el,wxproject_id);
					return;
				}
			}
		}
		
		if(content.length()>4000){
			 msg=file.getErrorMsg();
		     el.setAttr("msg",msg);
		     visit(el,wxproject_id);
		     return;
		}
		
		share_content.setContent(content);
		share_content.setPos(pos);
		String old_pic=share_content.getPic_url();
		if(pic!=null){
			share_content.setPic_url(ReplySetting.getRelatImagePath(el,wxproject_id, pic_file_name));
		}
		try{
			share_content_dao.update(share_content);
		}catch(Exception e){
			e.printStackTrace();
			el.setAttr("msg",el.i18("fdc_usercenter.server_error"));
			
			if(pic!=null){
				pic.delete();
			}
			
			visit(el,wxproject_id);
			return;
		}
		
		if(pic!=null&&StringHelper.isNotNull(old_pic)){
			File old_pic_file=new File(el.getRealPath(old_pic));
			old_pic_file.delete();
		}
		
		el.setAttr("msg",el.i18("operation.success"));
		visit(el,wxproject_id);
	}
	
	private void add_new_message(EventLoader el,long wxproject_id) throws IOException, ServletException{
		if(!el.isMultipartContent()){
			el.text("invalid visit.");
			return;
		}
		FilePostForm file=el.parseFilePostForm(ProjectConfig.getLong(wxproject_id,"sys.max_image_file_size")*1024);
		file.setAllParamToAttr(el);
		String content=file.getStr("content");
		long sa_id=file.getLong("sa_id");
		el.setParam("id",sa_id);
		String msg=null;
		if(file.getErrorMsg()!=null){
	        msg=file.getErrorMsg();
	        el.setAttr("msg",msg);
	        visit(el,wxproject_id);
	       return;
	    }
		String pic_file_name=null;
		FileItem pic=null;
		File pic_file=null;
		List<FileItem> items=file.getFileItems();
		for(FileItem it:items){
			if(it.getFieldName().toLowerCase().equals("pic")){
				pic=it;
			}
		}
		
		if(pic!=null){
			if(AppConfig.img_support_fix.get(StringHelper.postfix(pic.getName().toLowerCase()))==null){
				msg=String.format(el.i18("fdc_usercenter.need_upload_business_card"),Keys.getStr("fdc_usercenter.image_post_fix_support"));
				el.setAttr("msg",msg);
				visit(el,wxproject_id);
				return;
			}else{
				pic_file_name=UUID.randomUUID().toString()+".jpg";
		        String file_path=ReplySetting.getImagePath(el,wxproject_id, pic_file_name);
		        pic_file=new File(file_path);
		        if(!pic_file.getParentFile().exists()){
		        	pic_file.getParentFile().mkdirs();
		        }
		        try {
		        	pic.write(pic_file);
				} catch (Exception e) {
					e.printStackTrace();
					msg=el.i18("fdc_usercenter.image.save.failed");
					el.setAttr("msg",msg);
					visit(el,wxproject_id);
					return;
				}
			}
		}
		
		if(content.length()>4000){
			 msg=file.getErrorMsg();
		     el.setAttr("msg",msg);
		     visit(el,wxproject_id);
		     return;
		}
		ShareActivityContent share_content=new ShareActivityContent();
		share_content.setContent(content);
		
		share_content.setSa_id(sa_id);
		if(pic!=null)share_content.setPic_url(ReplySetting.getRelatImagePath(el,wxproject_id, pic_file_name));
		try{
			share_content.setPos(share_content_dao.getMaxPos(sa_id));
			share_content_dao.insert(share_content);
		}catch(Exception e){
			e.printStackTrace();
			el.setAttr("msg",el.i18("fdc_usercenter.server_error"));
			
			if(pic!=null){
				pic.delete();
			}
			
			visit(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("operation.success"));
		visit(el,wxproject_id);
	}
	
	private void update(EventLoader el,long wxproject_id) throws IOException, ServletException{
		if(!el.isMultipartContent()){
			el.text("invalid visit.");
			return;
		}
		FilePostForm post_form=el.parseFilePostForm(ProjectConfig.getLong(wxproject_id,"sys.max_image_file_size")*1024);
		long id=post_form.getLong("id");
		String name=post_form.getStr("name");
		String start=post_form.getStr("start");
		String end=post_form.getStr("end");
		String type=post_form.getStr("type");
		String template=post_form.getStr("template");
		String reward=post_form.getStr("reward");
		String rules=post_form.getStr("rules");
		
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		el.setParam("id",id);
		Date start_date=null;
		Date end_date=null;
		try {
			start_date=format.parse(start);
		} catch (ParseException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("share.add.starttime.format.wrong"));
			visit(el,wxproject_id);
			return;
		}
		
		try {
			end_date=format.parse(end);
		} catch (ParseException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("share.add.endtime.format.wrong"));
			visit(el,wxproject_id);
			return;
		}
		
		if(StringHelper.isNull(name)){
			el.setAttr("msg",el.i18("share.add.name.cannot.empty"));
			visit(el,wxproject_id);
			return;
		}
		
		if(StringHelper.isNull(template)){
			el.setAttr("msg",el.i18("share.add.template.cannot.empty"));
			visit(el,wxproject_id);
			return;
		}
		
		if(StringHelper.isNull(type)){
			el.setAttr("msg",el.i18("share.add.type.cannot.empty"));
			visit(el,wxproject_id);
			return;
		}
		
		ShareActivity activity=null;
		System.out.println(id);
		try {
			activity=share_dao.getById(id);
		} catch (SQLException e1) {
			e1.printStackTrace();
			el.setAttr("msg",el.i18("server.error")+":"+e1.getLocalizedMessage());
			visit(el,wxproject_id);
			return;
		}
		
		if(activity==null){
			el.setAttr("msg",el.i18("mc.share.not.exists"));
			index(el,wxproject_id);
			return;
		}

		String pic_file_name=null;
		FileItem pic=null;
		File pic_file=null;
		List<FileItem> items=post_form.getFileItems();
		for(FileItem it:items){
			if(it.getFieldName().toLowerCase().equals("pic")){
				pic=it;
			}
		}
		
		if(pic!=null){
			if(AppConfig.img_support_fix.get(StringHelper.postfix(pic.getName().toLowerCase()))==null){
				el.setAttr("msg",el.i18("mc.share.image_post_fix_not_support"));
				visit(el,wxproject_id);
				return;
			}else{
				pic_file_name=UUID.randomUUID().toString()+".jpg";
		        String file_path=ReplySetting.getImagePath(el,wxproject_id, pic_file_name);
		        pic_file=new File(file_path);
		        if(!pic_file.getParentFile().exists()){
		        	pic_file.getParentFile().mkdirs();
		        }
		        try {
		        	pic.write(pic_file);
				} catch (Exception e) {
					e.printStackTrace();
					el.setAttr("msg",el.i18("fdc_usercenter.image.save.failed"));
					visit(el,wxproject_id);
					return;
				}
			}
		}
		
		
		
		activity.setEnd(end_date);
		activity.setName(name);
		activity.setStart(start_date);
		activity.setTemplate(template);
		activity.setType(type);
		activity.setUpdate_time(Calendar.getInstance().getTime());
		activity.setId(id);
		activity.setRules(rules);
		activity.setReward(reward);
		
		String old_pic=activity.getFace_pic_url();
		
		if(pic!=null){
			activity.setFace_pic_url(ReplySetting.getRelatImagePath(el,wxproject_id, pic_file_name));
		}
		
		try {
			share_dao.update(activity);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("server.error")+":"+e.getLocalizedMessage());
			
			if(pic!=null){
				pic.delete();
			}
			
			visit(el,wxproject_id);
			return;
			
		}
		
		if(pic!=null&&StringHelper.isNotNull(old_pic)){
			File old_pic_file=new File(el.getRealPath(old_pic));
			old_pic_file.delete();
		}
		
		el.setAttr("msg",el.i18("operation.success"));
		
		visit(el,wxproject_id);
	}
	
	private void back_hits(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("id");
		ShareActivity share=null;
		try {
			share=share_dao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		if(share==null){
			el.setAttr("msg",el.i18("mc.share.not.exists"));
			index(el,wxproject_id);
			return;
		}
		int top=Keys.getInt("share.back_hits_annoucement_top");
		List<ShareBackHits> hits_list=null;
		try {
			hits_list=share_back_hits_dao.getBackHitsTopList(id,top);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		el.setAttr("hits_list",hits_list);
		el.setAttr("share",share);
		el.setAttr("status_list",ShareActivity.STATUS.values());
		el.template("/WEB-INF/template/boss/share/back_hits.vm");
	}
	
	private void announcement_now(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long id=el.getLong("id");
		ShareActivity share=null;
		try {
			share=share_dao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		if(share==null){
			el.setAttr("msg",el.i18("mc.share.not.exists"));
			index(el,wxproject_id);
			return;
		}
		int top=Keys.getInt("share.back_hits_annoucement_top");
		List<ShareBackHits> hits_list=null;
		try {
			hits_list=share_back_hits_dao.getBackHitsTopList(id,top);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		el.setAttr("hits_list",hits_list);
		el.setAttr("share",share);
		el.setAttr("status_list",ShareActivity.STATUS.values());
		el.template("/WEB-INF/template/boss/share/announcement.vm");
	}
	
	private void visit(EventLoader el,long wxproject_id) throws IOException, ServletException{
		
		long id=el.getLong("id");
		ShareActivity share=null;
		
		try {
			share=share_dao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		
		if(share==null){
			el.setAttr("msg",el.i18("mc.share.not.exists"));
			index(el,wxproject_id);
			return;
		}
		
		el.setAttr("share",share);
		
		List<ShareActivityContent> content_list=null;
		
		try {
			content_list=share_content_dao.getBySAId(share.getId());
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("operation.failed")+":"+e.getLocalizedMessage());
			index(el,wxproject_id);
			return;
		}
		
		el.setAttr("content_list", content_list);
		el.setAttr("status_list",ShareActivity.STATUS.values());
		el.template("/WEB-INF/template/boss/share/detail.vm");
	}
	
	private void add_new(EventLoader el) throws IOException, ServletException{
		
		el.setAttr("now_str",Calendar.getInstance().getTime().toLocaleString());
		el.setAttr("status_list",ShareActivity.STATUS.values());
		el.template("/WEB-INF/template/boss/share/add.vm");
	}
	
	private void add(EventLoader el,long wxproject_id) throws IOException, ServletException{
		if(!el.isMultipartContent()){
			el.text("invalid visit.");
			return;
		}
		FilePostForm post_form=el.parseFilePostForm(ProjectConfig.getLong(wxproject_id,"sys.max_image_file_size")*1024);
		post_form.setAllParamToAttr(el);
		String name=post_form.getStr("name");
		String start=post_form.getStr("start");
		String end=post_form.getStr("end");
		String type=post_form.getStr("type");
		String template=post_form.getStr("template");
		String reward=post_form.getStr("reward");
		String rules=post_form.getStr("rules");
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start_date=null;
		Date end_date=null;
		try {
			start_date=format.parse(start);
		} catch (ParseException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("share.add.starttime.format.wrong"));
			add_new(el);
			return;
		}
		
		try {
			end_date=format.parse(end);
		} catch (ParseException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("share.add.endtime.format.wrong"));
			add_new(el);
			return;
		}
		
		if(StringHelper.isNull(name)){
			el.setAttr("msg",el.i18("share.add.name.cannot.empty"));
			add_new(el);
			return;
		}
		
		if(StringHelper.isNull(template)){
			el.setAttr("msg",el.i18("share.add.template.cannot.empty"));
			add_new(el);
			return;
		}
		
		if(StringHelper.isNull(type)){
			el.setAttr("msg",el.i18("share.add.type.cannot.empty"));
			add_new(el);
			return;
		}
		
		String pic_file_name=null;
		FileItem pic=null;
		File pic_file=null;
		List<FileItem> items=post_form.getFileItems();
		for(FileItem it:items){
			if(it.getFieldName().toLowerCase().equals("pic")){
				pic=it;
			}
		}
		
		if(pic!=null){
			if(AppConfig.img_support_fix.get(StringHelper.postfix(pic.getName().toLowerCase()))==null){
				el.setAttr("msg",el.i18("mc.share.image_post_fix_not_support"));
				add_new(el);
				return;
			}else{
				pic_file_name=UUID.randomUUID().toString()+".jpg";
		        String file_path=ReplySetting.getImagePath(el,wxproject_id, pic_file_name);
		        pic_file=new File(file_path);
		        if(!pic_file.getParentFile().exists()){
		        	pic_file.getParentFile().mkdirs();
		        }
		        try {
		        	pic.write(pic_file);
				} catch (Exception e) {
					e.printStackTrace();
					el.setAttr("msg",el.i18("fdc_usercenter.image.save.failed"));
					add_new(el);
					return;
				}
			}
		}
		
		ShareActivity activity=new ShareActivity();
		activity.setStatus(ShareActivity.STATUS.ABOUT_START.getValue());
		activity.setEnd(end_date);
		activity.setName(name);
		activity.setStart(start_date);
		activity.setTemplate(template);
		activity.setType(type);
		activity.setReward(reward);
		activity.setRules(rules);
		activity.setWxproject_id(wxproject_id);
		activity.setUpdate_time(Calendar.getInstance().getTime());
		if(pic!=null)activity.setFace_pic_url(ReplySetting.getRelatImagePath(el,wxproject_id, pic_file_name));
		try {
			share_dao.insert(activity);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("server.error")+":"+e.getLocalizedMessage());
			add_new(el);
			return;
			
		}
		el.setAttr("msg",el.i18("operation.success"));
		
		index(el,wxproject_id);
	}
	
	private void index(EventLoader el,long wxproject_id) throws IOException, ServletException{
		int status=el.getInt("status",ShareActivity.STATUS.ABOUT_START.getValue());
		List<ShareActivity> share_list=null;
		
		try {
			share_list=share_dao.getByStatus(wxproject_id,status);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		el.setAttr("status", el.getInt("status",ShareActivity.STATUS.ABOUT_START.getValue()));
		el.setAttr("share_list", share_list);
		el.setAttr("status_list",ShareActivity.STATUS.values());
		el.template("/WEB-INF/template/boss/share/index.vm");
	}

}
